package com.ktjy.web.servlet;

import com.alibaba.fastjson.JSON;
import com.ktjy.web.entity.AjaxResult;
import com.ktjy.web.entity.AttrValues;
import com.ktjy.web.entity.Page;
import com.ktjy.web.service.AttrValuesService;
import com.ktjy.web.service.impl.AttrValuesServiceImpl;
import com.ktjy.web.util.BaseSevlet;
import com.ktjy.web.util.R;
import my.framework.anno.RequestMapping;
import org.apache.ibatis.annotations.Param;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

//@WebServlet(name = "AttrValuesServlet", urlPatterns = "/api/AttrValuesServlet")
public class AttrValuesServlet extends BaseSevlet {

    @RequestMapping("/AttrValues")
    public Object AttrValue(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String opr = request.getParameter("opr");
        AttrValuesService tmp = new AttrValuesServiceImpl();

        if("page".equals(opr)){
            String name=request.getParameter("name");
            String attrId=request.getParameter("attrId");

            if (attrId==""){
                attrId="0";
            }

            int pageNo=Integer.parseInt(request.getParameter("pageNo"));
            int pageSize=Integer.parseInt(request.getParameter("pageSize"));

            Page page=new Page();
            page.setPageSize(pageSize);
            page.setTotalCount(tmp.count());
            page.setList(tmp.page(name,Integer.parseInt(attrId),pageNo,pageSize));

            return AjaxResult.success("查询成功",page);

        }else if("del".equals(opr)){
            int id=Integer.parseInt(request.getParameter("id"));

            int num=tmp.deleteById(id);

            return AjaxResult.success("执行成功",num);

        }else if("detail".equals(opr)){
            int id=Integer.parseInt(request.getParameter("id"));

            AttrValues data=tmp.selectById(id);

            return AjaxResult.success("执行成功",data);
        }else if("update".equals(opr)){
            int id=Integer.parseInt(request.getParameter("id"));
            int attrId=Integer.parseInt(request.getParameter("attrId"));
            String name=request.getParameter("name");
            String desc=request.getParameter("desc");

            AttrValues attrValues=new AttrValues();

            attrValues.setId(id);
            attrValues.setAttrId(attrId);
            attrValues.setDesc(desc);
            attrValues.setName(name);

            int num =tmp.update(attrValues);

            return AjaxResult.success("执行成功",num);

        }else if("insert".equals(opr)){
            int attrId=Integer.parseInt(request.getParameter("attrId"));
            String name=request.getParameter("name");
            String desc=request.getParameter("desc");
            int orderNum=tmp.count()+1;

            AttrValues attrValues=new AttrValues();

            attrValues.setAttrId(attrId);
            attrValues.setName(name);
            attrValues.setDesc(desc);
            attrValues.setOrderNum(orderNum);

            int num =tmp.insert(attrValues);

            return AjaxResult.success("执行成功",num);

        }

        return null;
    }
}
